/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.front.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.front.domain.MediumStorageFront;
import me.zhengjie.front.domain.vo.MediumStorageQueryCriteria;
import me.zhengjie.front.mapper.MediumStorageFrontMapper;
import me.zhengjie.front.service.MediumStorageService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author XX
* @date 2024-05-23
**/
@Service
@RequiredArgsConstructor
public class MediumStorageServiceImpl extends ServiceImpl<MediumStorageFrontMapper, MediumStorageFront> implements MediumStorageService {

    private final MediumStorageFrontMapper mediumStorageFrontMapper;

    @Override
    public PageResult<MediumStorageFront> queryAll(MediumStorageQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(mediumStorageFrontMapper.findAll(criteria, page));
    }

    @Override
    public List<MediumStorageFront> queryAll(MediumStorageQueryCriteria criteria){
        return mediumStorageFrontMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MediumStorageFront resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MediumStorageFront resources) {
        MediumStorageFront mediumStorageFront = getById(resources.getId());
        mediumStorageFront.copy(resources);
        saveOrUpdate(mediumStorageFront);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<MediumStorageFront> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MediumStorageFront mediumStorageFront : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("储位编码", mediumStorageFront.getMediumStorageCode());
            map.put("所属大储位编码", mediumStorageFront.getLargeStorageCode());
            map.put("优先级", mediumStorageFront.getOrderNum());
            map.put("待分拣区记录中储位存放哪个订单", mediumStorageFront.getOutboundOrder());
            map.put("存放物料1", mediumStorageFront.getMaterialOne());
            map.put("入库单1", mediumStorageFront.getInboundOrderOne());
            map.put("批次号1", mediumStorageFront.getBatchOne());
            map.put("物料2", mediumStorageFront.getMaterialTwo());
            map.put("入库单2", mediumStorageFront.getInboundOrderTwo());
            map.put("批次2", mediumStorageFront.getBatchTwo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}